package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;


    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for (int i = 4; i > intCode.toString().length(); i--) {

            unitCode = "0" + unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        List<Goods> goodsList = goodsDao.getListInventory((page - 1) * rows, rows, codeOrName, goodsTypeId);
        int total = goodsDao.count();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", goodsList);
        return map;
    }



    @Override
    public Map<String, Object> unitList() {
        List<Unit> unitList = goodsDao.getUnitList();
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", unitList);
        return map;
    }

    @Override
    public Map<String, Object> goodList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        List<Goods> goodsList = goodsDao.getGoodsList((page - 1) * rows, rows, goodsName, goodsTypeId);
        int total = goodsDao.count();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", goodsList);
        return map;
    }

    @Override
    public void saveGoods(Goods goods) {
        if (goods.getGoodsId() != null) {
            //更新
            goodsDao.updateGoods(goods);
        } else {
            //插入
            goodsDao.saveGoods(goods);
        }
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        List<Goods> goodsList = goodsDao.getNoInventoryQuantity((page - 1) * rows, rows, nameOrCode);
        int total = goodsDao.countNoInventory();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", goodsList);
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        List<Goods> goodsList = goodsDao.getHasInventoryQuantity((page - 1) * rows, rows, nameOrCode);
        int total = goodsDao.countHasInventory();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", goodsList);
        return map;
    }

    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.saveStock(goodsId, inventoryQuantity, purchasingPrice);
    }

    //（要判断商品状态,入库、有进货和销售单据的不能删除）
    @Override
    public ServiceVO deleteGoods(Integer goodsId) {
        Goods goods =  goodsDao.selectById(goodsId);
        if (goods.getState() == 0) {
            //可以删除
            goodsDao.deleteGoods(goodsId);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }

        return  new ServiceVO<>(ErrorCode.STORED_ERROR_CODE, ErrorCode.STORED_ERROR_MESS);
    }

    //（要判断商品状态,入库、有进货和销售单据的不能删除）

    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        Goods goods =  goodsDao.selectById(goodsId);
        if (goods.getState() == 0) {
            //可以删除
            goods.setInventoryQuantity(0);
            goodsDao.updateGoods(goods);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }

        return  new ServiceVO<>(ErrorCode.STORED_ERROR_CODE, ErrorCode.STORED_ERROR_MESS);    }

    @Override
    public Map<String, Object> listAlarm() {

        List<Goods> goodsList = goodsDao.getListAlarm();
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", goodsList);
        return map;
    }

}
